<template>
  <el-dialog
    title="可跳节点"
    :visible.sync="showJump"
    width="30%"
    center>
    <div>
      <p>请选择可跳节点</p>
      <table class="oa-form__dialog-tab">
         <tr>
            <td>选择跳转步骤</td>
            <td>
              <el-radio-group v-model="j_step" @change="selectJumpFunc($event)">
              <el-radio :label="item.id" v-for="(item, index) in j_nodes" :key="index">{{item.name}}</el-radio>
            </el-radio-group>
            </td>
         </tr>
         <tr>
            <td>跳转节点主办人<em class="red" style="display:inline">*</em></td>
            <td>
              <!--特殊事项-->
              <el-radio-group v-model="j_main" v-if="showCommon">
                <el-radio :label="item.id" v-for="(item, index) in j_users" :key="index">{{item.truename}}</el-radio>
              </el-radio-group>
              <el-radio-group v-model="j_main" v-else>
                <el-radio :label="item.id" v-for="(item, index) in j_sp_user" :key="index">{{item.truename}}</el-radio>
              </el-radio-group>
            </td>
         </tr>
      </table>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancle">取 消</el-button>
      <el-button type="primary" @click="setJumpFunc">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { setJump } from '@/api/oa/index.js'
export default {
  data () {
    return {
      j_nodes: [],
      j_users: [],
      j_sp_user: [],
      j_main: '',
      j_step: 0,
      showCommon: true
    }
  },
  props: ['jumpData', 'showJump', 'approve'],
  watch: {
    jumpData () {
      if (this.jumpData.special_user && this.jumpData.special_user.length) {
        this.j_nodes.push(this.jumpData.special_node)
        this.j_sp_user = this.jumpData.special_user
        this.j_main = this.j_sp_user[0].id
        this.j_step = this.jumpData.special_node.id
      }
      if (this.jumpData.user && this.jumpData.user.length) {
        this.j_nodes = this.jumpData.nodes
        this.j_users = this.jumpData.user
        this.j_main = this.j_users[0].id
        this.j_step = this.j_nodes[0].id
      }
      if (this.jumpData.user && this.jumpData.user.length && this.jumpData.special_user && this.jumpData.special_user.length) {
        this.j_nodes = this.jumpData.nodes
        this.j_nodes.push(this.jumpData.special_node)
        this.j_users = this.jumpData.user
        this.j_main = this.j_users[0].id
        this.j_step = this.j_nodes[0].id
      }
    }
  },
  methods: {
    cancle () {
      this.$emit('childJump')
    },
    selectJumpFunc (e) {
      this.j_step = e
      if (this.j_step === 224 || this.j_step === 227 || this.j_step === 225 || this.j_step === 226 || this.j_step === 242) {
        this.showCommon = false
        this.j_main = this.j_sp_user[0].id
      } else {
        this.showCommon = true
        this.j_main = this.j_users[0].id
      }
    },
    setJumpFunc () {
      const params = {
        openid: window.localStorage.getItem('token'),
        approve_node_id: this.j_step,
        workflow_id: this.approve.workflow_id,
        jump_user_id: this.j_main,
        status: 100,
        opinion: '同意'
      }
      setJump(params).then(() => {
        this.$emit('childJump')
        this.$router.push({ name: 'oaList', query: { activeName: 3 } })
      })
    }
  }
}
</script>
